all: SimpleSection.c hello.c
	gcc -m32 -g0 -c SimpleSection.c
	objdump -x -h -s -d SimpleSection.o > SimpleSection.asm
	readelf -h SimpleSection.o > SimpleSection.elfheader
	readelf -S SimpleSection.o > SimpleSection.sections
	gcc -static -fno-builtin hello.c -o hello_static
	gcc -fno-builtin hello.c -o hello_nostatic
	ld -verbose > ld.script

tiny: TinyHelloWorld.c
	# must specify -m32
	gcc -c -m32 -fno-builtin TinyHelloWorld.c
	ld -static -m elf_i386 -e nomain -o TinyHelloWorld TinyHelloWorld.o
	# use -s to disable genaration of .symtab and .strtab in output file
	# use -T to specify ld script
	ld -static -m elf_i386 -s -T TinyHelloWorld.lds -o TinyHelloWorld_T TinyHelloWorld.o
	objdump -x -d -h TinyHelloWorld.o > TinyHelloWorld.o.asm
	objdump -x -d -h TinyHelloWorld > TinyHelloWorld.asm
	readelf -S TinyHelloWorld.o > TinyHelloWorld.o.sections
	readelf -S TinyHelloWorld > TinyHelloWorld.sections
	readelf -S TinyHelloWorld_T > TinyHelloWorld_T.sections

pic: pic.c pic_main.c
	gcc -m32 -shared -fPIC -o pic.so pic.c
	gcc -m32 -Wl,--rpath=. -o pic_main pic_main.c pic.so
	gcc -m32 -shared -o nopic.so pic.c
	objdump -D pic.so > pic.asm
	objdump -D nopic.so > nopic.asm
	objdump -D pic_main > pic_main.asm
	readelf -dsD pic.so > pic.dyn
	readelf -S pic.so > pic.sections

proc_stack: proc_stack.c
	gcc -m32 -o proc_stack proc_stack.c
	readelf -h proc_stack

run_so: RunSoSimple.c
	gcc -m32 -g -o RunSoSimple RunSoSimple.c -ldl
	./RunSoSimple /lib32/libm-2.31.so sqrt

shell: shell.c shell_lib1.c shell_lib2.c shell_client.c
	gcc -m32 -g -shared -fPIC -o shell_lib1.so shell_lib1.c
	gcc -m32 -g -shared -fPIC -o shell_lib2.so shell_lib2.c
	@# -export-dynamic表示将shell.c中的所有全局符号保存到.dynsym中, 否则shell.c中未被其他共享对象引用的全局符号将不会保存到.dynsym, 在dlsym查找符号时也就找不到这些符号
	gcc -m32 -g -Wl,-rpath=. -Wl,-export-dynamic -o shell shell.c shell_lib1.so shell_lib2.so -ldl -lpthread
	gcc -m32 -g -o shell_client shell_client.c -lpthread

preload: preload1.c preload2.c preload_main.c
	gcc -m32 -g -shared -fPIC -o preload1.so preload1.c
	gcc -m32 -g -shared -fPIC -o preload2.so preload2.c
	gcc -m32 -g -o preload -Wl,-rpath=. preload_main.c preload2.so
	@echo "=== normal load ==="
	./preload
	@echo "=== pre load ==="
	LD_PRELOAD=./preload1.so ./preload
	@echo "=== load debug ==="
	LD_DEBUG=all LD_PRELOAD=./preload1.so ./preload > preload.debug 2>&1

init_fini: init_fini.c init_fini_main.c
	gcc -m32 -g -shared -fPIC -o init_fini.so init_fini.c
	gcc -m32 -g -o init_fini -Wl,-rpath=. init_fini_main.c init_fini.so
	objdump -d init_fini.so > init_fini.asm
	@echo "=== test shared library .init and .fini ==="
	./init_fini

stack: stack.c
	gcc -m32 -c stack.c
	objdump -d stack.o > stack.asm

call_convention: call_convention.c call_convention_main.c
	gcc -m32 -o call_convention call_convention.c call_convention_main.c
	./call_convention

cpp_init: cpp_init.cpp
	g++ -o cpp_init cpp_init.cpp

atexit: atexit.c
	gcc -m32 -o atexit atexit.c

crt: crt.cpp ctor.c
	g++ -m32 -save-temps -o crt crt.cpp
	objdump -D crt > crt.asm
	objdump -D crt.o > crt.o.asm
	objdump -D crt1.o > crt1.asm
	objdump -D crti.o > crti.asm
	objdump -D crtn.o > crtn.asm
	objdump -D crtbegin.o > crtbegin.asm
	objdump -D gcrt1.o > gcrt1.asm
	g++ -m32 -save-temps -o ctor ctor.c
	objdump -D ctor > ctor.asm

clean:
	ls -l | grep -vE '\.c|\.h|crt.*.o|gcrt.*.o|ld.script|\.lds|Makefile' | awk '{print $$9}' | xargs rm -vf
